'use strict';

// 日期选择插件(自定义)

var date = function ($) {

  $.fn.hotelDate = function (options,callback) {
    var a = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
    var nowdate = !a.test(options.start_date)? new Date() : new Date(options.start_date);
    var date_amount = parseInt(options.date_amount);
    isNaN(date_amount) ? date_amount = 1 : false;
    // var nowdate = new Date(); // 获取当前时间
    var dateArr = new Array(); // 获取到的时间数据集合
    var btn = $(this);
    var num = 0;
    btn.on('click', initTemplate); // 初始化(显示)插件
    // 初始化模板
      function initTemplate() {
          var entertime = $(btn).attr('data-entertime');
          if(options.date != 2) var leavetime = $(btn).attr('data-leavetime');
          var listIndex = 0;
          $('body').css({
              overflow: 'hidden'
          });
          if(options.is_train=1){
              // 主容器模板
              //<i class="aui-iconfont aui-icon-correct"></i>
              var dateTemplate = '\n<div class="aui-mask aui-mask-in" id="mask"></div>\n        <div class =\'date container c-gray\'>\n          <div class="tac bold title_box">\u8BF7\u9009\u62E9<span class=\'c-blue\'>出发</span>\u65F6\u95F4 <div class=\'close-btn\'>确定</div></div>\n                  </div>      \n      ';
          }else{
              // 主容器模板
              var dateTemplate = '\n<div class="aui-mask aui-mask-in" id="mask"></div>\n        <div class =\'date container c-gray\'>\n          <div class="tac bold title_box">\u8BF7\u9009\u62E9<span class=\'c-blue\'>\u5165\u4F4F</span>\u548C<span class=\'c-red\'>\u79BB\u5F00</span>\u65F6\u95F4 <div class=\'close-btn\'> <i class="aui-iconfont aui-icon-correct"></i>     </div>\n</div>\n                  </div>      \n      ';
          }
          $('body').append(dateTemplate); // 向body添加插件

          // action容器模板
          dateArr.forEach(function (item, index) {
              var template = '\n          <div class=\'action mt10 action_box\'>\n            <div class=\'title tac c-blue\'><div class="y">' + item.getFullYear() + '</div>\u5E74<div class="m">' + (item.getMonth() + 1) + '</div>\u6708</div>\n            <ul class=\'week border-bottom\'><li>\u65E5</li><li>\u4E00</li><li>\u4E8C</li><li>\u4E09</li><li>\u56DB</li><li>\u4E94</li><li>\u516D</li></ul>\n            <ul class=\'day f-small\'></ul>\n          </div>        \n        ';
              $('.date').append(template);
          });

          // 天数模板
          $('.action').each(function (index, item) {
              var days = getDays(dateArr[index]); // 当月天数
              var nowweek = dateArr[index].getDay(); // 当月1号是星期几
              for (var i = 0; i < days + nowweek; i++) {
                  var template = '';
                  // 空白填充
                  if (i < nowweek) {
                      template = '<li class="no"></li>';
                  } else if (i < new Date().getDate() + nowweek - 1 && dateArr[index].getMonth() === new Date().getMonth()) {
                      // 当月已经过去的日期 不能点击
                      listIndex++;
                      template = '<li index=\'' + listIndex + '\' class=\'disable\'>' + (i - nowweek + 1) + '</li>';
                  } else if (dateArr[index].getMonth() == Number(entertime.split('/')[0]) - 1 && i - nowweek + 1 == Number(entertime.split('/')[1])) {
                      // 默认入住时间
                      listIndex++;
                      template = '<li index=\'' + listIndex + '\' class=\'enter\' date-date="' + dateArr[index].getFullYear() + '-' + (dateArr[index].getMonth() + 1) + '-' + (i - nowweek + 1) + '">' + (i - nowweek + 1) + '</li>';
                  } else if (options.date != 2 && dateArr[index].getMonth() == Number(leavetime.split('/')[0]) - 1 && i - nowweek + 1 == Number(leavetime.split('/')[1])) {
                      // 默认离开时间
                      listIndex++;
                      template = '<li index=\'' + listIndex + '\' class=\'leave\' date-date="' + dateArr[index].getFullYear() + '-' + (dateArr[index].getMonth() + 1) + '-' + (i - nowweek + 1) + '">' + (i - nowweek + 1) + '</li>';
                  } else {
                      listIndex++;
                      template = '<li index=\'' + listIndex + '\' date-date="' + dateArr[index].getFullYear() + '-' + (dateArr[index].getMonth() + 1) + '-' + (i - nowweek + 1) + '">' + (i - nowweek + 1) + '</li>';
                  }
                  $(item).find('.day').append(template);
              }
              var H = $('.action_box').outerHeight(true)+$('.action_box div').eq(0).outerHeight(true)+30
              $('.date').stop().animate({height:H});
          });

          // 事件监听
          // 关闭插件
          $('.close-btn').on('click', function () {
              // 获取入住时间
              var enterYear = $('.enter').parents('.day').siblings('.title').find('.y').text();
              var enterMonth = $('.enter').parents('.day').siblings('.title').find('.m').text();
              enterMonth.length === 1 ? enterMonth = '0' + enterMonth : false;
              var enterDay = $('.enter').text();
              enterDay.length === 1 ? enterDay = '0' + enterDay : false;
              var enterTime = enterMonth + '/' + enterDay;

              var ret = {};
              btn.attr('data-entertime',enterTime);
              ret.enterTime = enterYear + '/' + enterTime;
              ret.train_month= enterMonth;//火车票预定日期月
              ret.train_day= enterDay;//火车票预定日期日
              $("#my-startDate").val(ret.train_month+"月12312"+ret.train_day+"日");
              if(options.date != 2){
                  // 获取离开时间
                  var leaveYear = $('.enter').parents('.day').siblings('.title').find('.y').text();
                  var leaveMonth = $('.leave').parents('.day').siblings('.title').find('.m').text();
                  leaveMonth.length === 1 ? leaveMonth = '0' + leaveMonth : false;
                  var leaveDay = $('.leave').text();
                  leaveDay.length === 1 ? leaveDay = '0' + leaveDay : false;
                  var leaveTime = leaveMonth + '/' + leaveDay;
                  var night = Number($('.leave').attr('index')) - Number($('.enter').attr('index'));
                  btn.attr('data-leavetime',leaveTime);
                  ret.leaveTime = leaveYear + '/' + leaveTime;
                  ret.altogether = night;
              }
              ret.end_month = leaveMonth;
              ret.end_day = leaveDay;
              callback(ret);
              $('.date').remove(); // 移除插件
              $('#mask').remove(); //移除遮罩
              $('body').css({ overflow: 'auto' });

          });

          //模版点击事件
          $('#mask').on('click',function(){
              $('.date').stop().animate({height:'0px'},function(){
                  $('.date').remove(); // 移除插件
                  $('#mask').remove(); //移除遮罩
              });
              $('body').css({"overflow":"auto"});
          });

          // 时间选择
          if(options.date == 2){
              $('.day li').not('.no').on('click',date);
          }else{
              $('.day li').not('.no').on('click',date_section);
          }

      }
      function date_section () {
          if (!$(this).hasClass('disable')) {
              var thisIndex = Number($(this).attr('index'));
              var enterIndex = Number($('.enter').attr('index'));
              var leaveIndex = Number($('.leave').attr('index'));
              if (num % 2 === 0) {
                  removeLeave();
                  $(this).addClass('leave');
                  num++;
              } else if (num % 2 !== 0) {
                  removeEnter();
                  $(this).addClass('enter');
                  num++;
              }
              // 当入住时间 大于 离开时间 两者互换
              if (Number($('.enter').attr('index')) > Number($('.leave').attr('index'))) {
                  $('.day .enter')[0].className = 'leave';
                  $('.day .leave')[0].className = 'enter';
              }
          }
      }
      function date () {
          if (!$(this).hasClass('disable')) {
              removeEnter();
              removeLeave();
              $(this).addClass('enter');
          }
      }
      function removeLeave() {
          $('.day li').removeClass('leave');
      }

    function removeEnter() {
      $('.day li').removeClass('enter');
    }

    // 获取num个月的时间数据
    function getDate(num) {

      var year = new Date().getFullYear();
      var month = new Date().getMonth() - 1;

      for (var i = 0; i < num; i++) {
        month <= 12 ? month++ : (month = 1, year++);
        var data = new Date(year, month); // 从当前月开始算 一共个6个月的数据
        dateArr.push(data);
      }
    }

    // 获取当月天数
    function getDays(date) {
      //构造当前日期对象
      var date = date;
      //获取年份
      var year = date.getFullYear();
      //获取当前月份
      var mouth = date.getMonth() + 1;
      //定义当月的天数；
      var days;
      //当月份为二月时，根据闰年还是非闰年判断天数
      if (mouth == 2) {
        days = year % 4 == 0 ? 29 : 28;
      } else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
        //月份为：1,3,5,7,8,10,12 时，为大月.则天数为31；
        days = 31;
      } else {
        //其他月份，天数为：30.
        days = 30;
      }
      return days;
    }

    function initDay() {
      var enterYear = String(nowdate.getFullYear());
      var enterMonth = String(nowdate.getMonth() + 1);
      enterMonth.length === 1 ? enterMonth = '0' + enterMonth : false;
      var enterDay = String(nowdate.getDate());
      enterDay.length === 1 ? enterDay = '0' + enterDay : false;
      var enterTime = enterMonth + '/' + enterDay;
      // 获取离开时间
      var leaveDate = new Date(new Date(nowdate).valueOf() + (60 * 60 * 24 * date_amount *  1000));
      var leaveDay = leaveDate.getDate();
      var leaveMonth = leaveDate.getMonth() + 1;
      var leaveYear = leaveDate.getFullYear();
      leaveMonth.toString().length == 1 ? leaveMonth   = '0' + leaveMonth.toString()   : false ;
      leaveDay.toString().length   == 1 ? leaveDay     = '0' + leaveDay.toString()     : false ;

      // var leaveYear = String(nowdate.getFullYear());
      // var leaveMonth = String(nowdate.getMonth() + 1);
      // leaveMonth.length === 1 ? leaveMonth = '0' + leaveMonth : false;
      // var leaveDay = String(nowdate.getDate() + 1);
      // console.log(leaveDay);
      // leaveDay.length === 1 ? leaveDay = '0' + leaveDay : false;
      var leaveTime = leaveMonth + '/' + leaveDay;
      btn.attr('data-entertime',enterTime);
      var ret = {};
      ret.enterTime = enterYear + '/' + enterTime;
      ret.train_month= enterMonth;//火车票预定日期月
      ret.train_day= enterDay;//火车票预定日期日
      ret.end_month = leaveMonth;
      ret.end_day = leaveDay;
      if(options.date != 2){
          btn.attr('data-leavetime',leaveTime);
          ret.leaveTime = leaveYear + '/' + leaveTime;
          ret.altogether = date_amount;
      }
      callback(ret);
      $('.input-enter').val(enterYear + '/' + enterTime);
      $('.input-leave').val('—'+leaveYear + '/' + leaveTime);
    }

    getDate(6); // 获取数据 参数: 拿6个月的数据
    initDay(); // 初始化时间
  };
}(jQuery);
